/**
 * 
 */
package info.niwota.web.apache;

import info.niwota.web.Debug;
import info.niwota.web.Session;
import info.niwota.web.SessionManager;

import java.io.IOException;

import x.org.apache.http.HttpException;
import x.org.apache.http.HttpResponse;
import x.org.apache.http.HttpResponseInterceptor;
import x.org.apache.http.protocol.HttpContext;
import android.util.Log;

/**
 * @author qiangli
 *
 */
public class SessionResponseInterceptor implements HttpResponseInterceptor {

	private static final String TAG = "SessionResponseInterceptor";

	public SessionResponseInterceptor() {
	}

	public void process(HttpResponse response, HttpContext context)
			throws HttpException, IOException {
		//skip proxy request
		if (HttpUtils.isProxy(context)) {
			return;
		}
		
		Session session = (Session) context.getAttribute(SessionManager.SESSION_KEY);
		if (Debug.DEBUG) {
			Log.d(TAG, "process session: " + session);
		}
		if (session != null) {
			String sid = session.id;
			HttpUtils.setSessionCookie(response, sid);
			if (Debug.DEBUG) {
				Log.d(TAG, "process session id: " + sid);
			}
		}
	}
}
